package com.woniu.product.app.apiImpl;

import cn.hutool.core.bean.BeanUtil;
import com.woniu.product.client.ProductManageClient;
import com.woniu.product.client.param.AddProductParam;
import com.woniu.product.client.param.AddSkuParam;
import com.woniu.product.client.param.DeductStockParam;
import com.woniu.product.client.param.RestoreStockParam;
import com.woniu.product.common.util.OssUtil;
import com.woniu.product.infra.model.SkuAttachment;
import com.woniu.product.infra.model.SpuAttachment;
import com.woniu.product.service.SkuService;
import com.woniu.product.service.SpuAttachmentService;
import com.woniu.product.service.SpuService;
import com.woniu.product.service.form.AddProductForm;
import com.woniu.product.service.form.DeductStockServiceParam;
import com.woniu.product.service.form.RestoreStockServiceParam;
import com.woniu.result.Result;
import com.woniu.util.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

@RestController
@Slf4j
public class ProductManageApiImpl implements ProductManageClient {
    @Resource
    private SpuAttachmentService spuAttachmentService;
    @Resource
    private SpuService spuService;
    @Resource
    private SkuService skuService;


    /**
     * 扣减库存
     *
     * @param deductStockParam
     * @return
     * @throws Exception
     */
    @Override
    public Result deductStock(DeductStockParam deductStockParam) throws Exception {
        log.info("扣减库存；{}", deductStockParam);
        DeductStockServiceParam deductStockServiceParam = BeanUtil.copyProperties(deductStockParam, DeductStockServiceParam.class);
        skuService.deductStock(deductStockServiceParam);
        log.info("扣减成功");
        return Result.getInstance();
    }

    /**
     * 还原库存
     *
     * @param restoreStockParam
     * @return
     * @throws Exception
     */
    @Override
    public Result restoreStock(RestoreStockParam restoreStockParam) throws Exception {
        log.info("还原库存；{}", restoreStockParam);
        RestoreStockServiceParam restoreStockServiceParam = BeanUtil.copyProperties(restoreStockParam, RestoreStockServiceParam.class);
        skuService.restoreStock(restoreStockServiceParam);
        log.info("还原库存");
        return Result.getInstance();
    }

    @Override
    public Result addProduct(AddProductParam addProduct) {
        log.info("添加商品spu:{}", addProduct);
        AddProductForm addProductForm = BeanUtil.copyProperties(addProduct, AddProductForm.class);
        spuService.addProduct(addProductForm);
        return Result.getInstance().setCode(200).setMsg("新增成功");
    }

    @Override
    public Result addSku(AddSkuParam addSkuParam) {
        log.info("增加sku:{}", addSkuParam);
        skuService.addSku(addSkuParam);
        return Result.getInstance().setCode(200).setMsg("新增成功");
    }


    @Override
    public Result SPUuploadFile(MultipartFile file) throws IOException {
        log.info("查看文件：{}", file);
        String filename = file.getOriginalFilename();
        String type = filename.substring(filename.lastIndexOf("."));
        String name = "lxlg" + System.currentTimeMillis() + type;
        String path = OssUtil.uploadFile("jianzhitailang", name, file.getInputStream());
        SpuAttachment spuAttachment = new SpuAttachment();
        spuAttachment.setOldfileName(filename);
        spuAttachment.setFilePath(path);
        spuAttachment.setFileSuffix(type);
        spuAttachment.setCreateDate(DateUtil.parseDateLong());
        spuAttachment.setFileName(name);
        spuAttachment.setSize(file.getSize());
        spuAttachmentService.save(spuAttachment);
        return Result.getInstance(String.class).setData(path).setCode(200).setMsg("上传成功");
    }

    @Override
    public Result SKUuploadFile(MultipartFile file) throws IOException {
        log.info("查看文件：{}", file);
        String filename = file.getOriginalFilename();
        String type = filename.substring(filename.lastIndexOf("."));
        String name = "lxlg" + System.currentTimeMillis() + type;
        String path = OssUtil.uploadFile("jianzhitailang", name, file.getInputStream());
        SkuAttachment skuAttachment = new SkuAttachment();
        skuAttachment.setOldfileName(filename);
        skuAttachment.setFilePath(path);
        skuAttachment.setFileSuffix(type);
        skuAttachment.setCreateDate(DateUtil.parseDateLong());
        skuAttachment.setFileName(name);
        skuAttachment.setSize(file.getSize());
        return Result.getInstance(String.class).setData(path).setCode(200).setMsg("上传sku附件成功");
    }

    @Override
    public Result upsku(Integer skuId) {
        log.info("上架商品：{}", skuId);
        return skuService.upsku(skuId);
    }

    @Override
    public Result downsku(Integer skuId) {
        log.info("下架商品：{}", skuId);
        return skuService.downsku(skuId);
    }

    @Override
    public Result downSPU(Integer spuId) {
        log.info("下架商品：{}", spuId);
        return spuService.downSPU(spuId);
    }

    @Override
    public Result upSPU(Integer spuId) {
        log.info("上架商品：{}", spuId);
        return spuService.upSPU(spuId);
    }

    @Override
    public Result delSPU(Integer spuId) {
        log.info("删除商品：{}", spuId);
        return spuService.delSPU(spuId);
    }

    @Override
    public Result delSku(Integer skuId) {
        log.info("删除商品：{}", skuId);
        return skuService.delsku(skuId);
    }

    /**
     * 增加销量
     *
     * @param skuId
     * @param count
     * @return
     */
    @Override
    public Result addSkuSalesCount(Integer skuId, Integer count) {
        log.info("增加销量：{}{}", skuId, count);
        skuService.addSkuSalesCount(skuId, count);
        return Result.getInstance();
    }



    /**
     * 扣减销量
     *
     * @param skuId
     * @param count
     * @return
     */
    @Override
    public Result subtractSkuSalesCount(Integer skuId, Integer count) {
        log.info("扣减销量：{}{}", skuId, count);
        skuService.subtractSkuSalesCount(skuId, count);
        return Result.getInstance();
    }

}
